using System;
using System.ComponentModel;
using System.Windows.Controls;
using NUnit.Framework;
using WPFUtilities;

namespace WPFUtilitiesUnitTests
{
    [TestFixture]
    public class AssertBindingTests
    {
        private TextBox boundBox;
        private TextBox unBoundBox;
        private Source source;

        [SetUp]
        public void CreateBinding()
        {
            source = new Source();
            source.Name = "Kablooie";
            boundBox = new TextBox();
            boundBox.DataContext = source;
            boundBox.SetBinding(TextBox.TextProperty, "Name");
            unBoundBox = new TextBox();
        }

        [Test]
        public void IsDataBoundFindsTheBinding()
        {
            AssertBinding.IsDataBound(boundBox, TextBox.TextProperty);
        }

        [Test]
        public void IsDataboundFailsWhenThePropertyIsNotBound()
        {
            try
            {
                AssertBinding.IsDataBound(unBoundBox, TextBox.TextProperty);
                Assert.Fail("Exception should have been thrown");
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Text is not databound.", ex.Message);
            }
        }

        [Test]
        public void IsBoundToPassesWhenTheBindingMatches()
        {
            AssertBinding.IsBoundToPath(boundBox, TextBox.TextProperty, "Name");
        }

        [Test]
        [ExpectedException(typeof(AssertionException))]
        public void IsBoundToFailsWhenThePathDoesNotMatch()
        {
            AssertBinding.IsBoundToPath(boundBox, TextBox.TextProperty, "Kaboom");
        }

        [Test]
        public void IsBoundToSource()
        {
            AssertBinding.IsSourceEqual(boundBox, TextBox.TextProperty, source);
        }
    }

    public class Source : INotifyPropertyChanged
    {
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
